package com.hankol.newretail.adapter

import android.widget.TextView
import androidx.core.content.ContextCompat
import com.baselibrary.extension.checkNotNull
import com.baselibrary.extension.checkValue
import com.baselibrary.extension.load
import com.baselibrary.extension.setMallPriceTTF
import com.baselibrary.utils.AmountUtil
import com.baselibrary.view.RoundCornerImageView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.hankol.newretail.R
import com.hankol.newretail.bean.MallOrderSkuDtoBean
import com.baselibrary.utils.SpecUtils

/*订单详情 商品列表 */
class OrderGoodListAdapter : BaseQuickAdapter<MallOrderSkuDtoBean, BaseViewHolder>(R.layout.item_order_detail_goods) {
    //状态：0-待付款（创建），1-待发货（已付款），2-待收货（已发货），3-待评价（已收货），5-已完成，6-已关闭
    var status = -1
    fun setOrderStatus(status: Int) {
        this.status = status
    }

    override fun convert(helper: BaseViewHolder, item: MallOrderSkuDtoBean) {
        helper.apply {
            item.let {
                getView<RoundCornerImageView>(R.id.img_cover).load(mContext, it.skuCover.checkValue())
                setText(R.id.tv_name, it.spuName.checkValue())
                getView<TextView>(R.id.tv_price).setMallPriceTTF(mContext)
                getView<TextView>(R.id.tv_unit).setMallPriceTTF(mContext)
                setText(R.id.tv_price, if (item.giftType == 1) "赠品" else "${AmountUtil.changeF2YKeepTwo("${it.price}")}")
                setText(R.id.tv_spec, SpecUtils.getNormsStr(it.specList))
                addOnClickListener(R.id.tv_apply_after)
                setGone(R.id.v_bottom, adapterPosition == data.size - 1)
                setGone(R.id.iv_gift_label, item.giftType == 1)
                //赠品 隐藏单位
                setGone(R.id.tv_unit, item.giftType == 0)
                if (status == 1 || status == 2) {//隐藏数量，显示售后按钮
                    setApplyAfterStaus(helper, it)
                } else {
                    if (status == 5) {//已完成，如果有售后状态 则显示
                        if (it.afterSale.checkNotNull()) {
                            //显示售后状态
                            setApplyAfterStaus(helper, it)
                        } else {
                            setGone(R.id.tv_apply_after, false)
                            setGone(R.id.tv_num, true)
                            setText(R.id.tv_num, "数量：${it.num}")
                        }
                    } else {
                        setGone(R.id.tv_num, true)
                        setText(R.id.tv_num, "数量：${it.num}")
                        setGone(R.id.tv_apply_after, false)
                    }
                }
            }
        }
    }

    private fun BaseViewHolder.setApplyAfterStaus(helper: BaseViewHolder, bean: MallOrderSkuDtoBean) {
        setGone(R.id.tv_num, false)
        setGone(R.id.tv_apply_after, bean.giftType == 0)
        val tvApplyAfter = helper.getView<TextView>(R.id.tv_apply_after)
        bean.afterSale?.let {
            tvApplyAfter.setTextColor(ContextCompat.getColor(mContext, R.color.color_D53))
            tvApplyAfter.setBackgroundResource(R.drawable.bg_stroke_f54d53_r4)
            setText(R.id.tv_spec, "${SpecUtils.getNormsStr(bean.specList)} ${"数量 ${bean.num}"}")
            when (it.afterSaleStatus) {
                0 -> {
                    //售后处理中
                    tvApplyAfter.setText("售后处理中")
                }
                1 -> {
                    //售后处理中
                    tvApplyAfter.setText("售后同意")
                }
                2 -> {
                    //售后处理中
                    tvApplyAfter.setText("售后拒绝")
                }
            }
        }
        if (bean.afterSale == null) {// else  还原 售后
            tvApplyAfter.setText("售后")
            tvApplyAfter.setTextColor(ContextCompat.getColor(mContext, R.color.color_666666))
            tvApplyAfter.setBackgroundResource(R.drawable.bg_stroke_e9e9e9_r4)
            if (bean.giftType == 1){
                //赠品
                setText(R.id.tv_spec, "${SpecUtils.getNormsStr(bean.specList)}")
                setGone(R.id.tv_num, true)
                setText(R.id.tv_num, "数量：${bean.num}")
            }else {
                setText(R.id.tv_spec, "${SpecUtils.getNormsStr(bean.specList)} 数量 ${bean.num}")
            }
        }
    }

}